package com.asm.server.handler;

import com.asm.message.GroupMembersRequestMessage;
import com.asm.message.GroupMembersResponseMessage;
import com.asm.server.session.GroupSession;
import com.asm.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

@ChannelHandler.Sharable
public class GroupMembersRequestHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        final GroupSession groupSession = GroupSessionFactory.getGroupSession();

        final List<Channel> channelList = groupSession.getMembersChannel(msg.getGroupName());
        GroupMembersResponseMessage resp
                = new GroupMembersResponseMessage(groupSession.getMembers(msg.getGroupName()));
        for (Channel channel : channelList) {
            channel.writeAndFlush(resp);
        }
    }
}
